package com.hmdp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.entity.User;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.SelectKey;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    private final StringRedisTemplate stringRedisTemplate;
    private final IUserService userService;
    @Override
    public Result follow(Long followId, Boolean isFollow) {
        //判断前端传来的isFollow是true or false
        UserDTO userDTO = UserHolder.getUser();
        Long userId = userDTO.getId();
        String key ="follows: " + userId;
        if(Boolean.TRUE.equals(isFollow)){
            //true 关注
            Follow follow = new Follow();
            follow.setUserId(userId);//关注者id
            follow.setFollowUserId(followId);//被关注者id
            boolean isSuccess = save(follow);
            if (isSuccess) {
                stringRedisTemplate.opsForSet().add(key, followId.toString());
                log.info("关注成功");
            }
        }else {
            //false 取消关注
            boolean isSuccess = remove(new QueryWrapper<Follow>().eq("user_id", userId).eq("follow_user_id", followId));
            if (isSuccess) {
                stringRedisTemplate.opsForSet().remove(key, followId.toString());
                log.info("取消关注成功");
            }
        }
        return Result.ok();
    }

    @Override
    public Result isFollow(Long followId) {
        UserDTO userDTO = UserHolder.getUser();
        Long userId = userDTO.getId();
        Long count = lambdaQuery().eq(Follow::getUserId, userId).eq(Follow::getFollowUserId, followId).count();
        return Result.ok(count>0);
    }

    @Override
    public Result followCommons(Long id) {
        // 获取当前用户id
        String key1 = "follows: " + UserHolder.getUser().getId();
        //前端传过来的参数id是我在看的用户主页的用户id
        String key2 = "follows: " + id;
        //求交集,结果为相同交际用户id，但为字符串
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key1, key2);
        //转为long型
        if (intersect == null || intersect.isEmpty()) {
            return Result.ok(Collections.emptyList());
        }
        Set<Long> userIds = intersect.stream().map(Long::valueOf).collect(Collectors.toSet());
        return Result.ok(userService.listByIds(userIds));
    }
}
